<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'
//数据模型 一个数组三个js对象
const family = ref([])
//声明一个异步的函数 页面加载完成就得调用方法展示查询结果 调用函数时需要同步等待返回结果
import {familyListService,familyAddService,familyUpdateService,familyDeleteService} from '@/api/article.js'
const familyList = async() =>{
    let result = await familyListService()
    //没有问题就返回文章分类列表 后端返回数据data数组类型
    family.value = result.data
}
familyList();//调用
//401没有携带token过来访问后台时 向后台发送请求时需携带token


//控制添加分类弹窗 控制弹窗显示默认false
const dialogVisible = ref(false)

//添加分类数据模型
const familyModel = ref({
    familyNumber: '',
    familyIncome: '',
    familyStuff:'',
    familyStatus:'',
    createtime:''
})
//添加分类表单校验
const rules = {
    familyNumber: [
        { required: true, message: '请输入家庭人数', trigger: 'blur' },
    ],
    familyIncome: [
        { required: true, message: '请输入家庭收入', trigger: 'blur' },
    ]
}

/*
//调用接口添加表单
import { ElMessage } from 'element-plus'
const addFamily = async() =>{
    //调用接口
    let result = await familyAddService(familyModel.value)
    ElMessage.success(result.msg?result.msg:'添加成功')

    //刷新一下 获取所有文章分类
    familyList();
    dialogVisible.value=false;
}
*/
//定义变量控制标题的展示
const title = ref('')

//展示编辑弹窗 row用来接收点击编辑时这一行的数据
//一点击编辑按钮，familyModel.value的各项就自动=row的各项 再把familyModel传给updateFamily就可给后台完成修改
const showDialog = (row) =>{
    dialogVisible.value = true;//放进标签里不用.value
    title.value='编辑家庭'
    //数据拷贝 修改时的回写也就是展示出来在这一行的数据在修改弹窗上
    familyModel.value.familyNumber=row.familyNumber;
    familyModel.value.familyIncome=row.familyIncome;
    familyModel.value.familyId = row.familyId;//扩展id属性将来传递给后台完成修改

}
    

//编辑家庭
const updateFamily = async()=>{
    //调用接口
    let result = await familyUpdateService(familyModel.value)
    ElMessage.success(result.msg?result.msg:'修改成功 ')

    familyList();
    dialogVisible.value=false;
}

//清空模型的数据 防止编辑之后添加家庭会数据回写 点击添加家庭时数据清空
const clearData = () =>{
    familyModel.value.familyNumber='',
    familyModel.value.familyIncome=''
}

import { ElMessageBox } from 'element-plus'
const deleteFamily = (row) =>{
    //提示用户 cv
    ElMessageBox.confirm(
    '确认删除吗?',
    '提示',
    {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
    }
    )
    .then(async() => {
        //调用接口
        let result = await familyDeleteService(row.familyId);
    ElMessage({
        type: 'success',
        message: '删除成功',
    })
    //刷新列表
    familyList();
    })
    .catch(() => {
    ElMessage({
        type: 'info',
        message: '删除取消',
    })
    })
    //cv

    }
    




import { Plus } from '@element-plus/icons-vue'
//控制抽屉是否展示
const visibleDrawer = ref(false)
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>家庭补助</span>
                <div class="extra">
                    <el-button type="primary" @click="visibleDrawer=true;title='添加家庭';clearData()">添加家庭</el-button>
                </div>
            </div>
        </template>
        <!--数据绑定 展示用的是后端传来的result赋值给了family数组-->
        <el-table :data="family" style="width: 100%">
            <el-table-column label="序号" width="100" type="index"> </el-table-column>
            <!--prop将categoryname绑定到这一分列上-->
            <el-table-column label="家庭人数" prop="familyNumber"></el-table-column>
            <el-table-column label="家庭收入" prop="familyIncome"></el-table-column>
            <el-table-column label="家庭补助金额" prop="familyAmount"></el-table-column>
            <el-table-column label="家庭资料" prop="familyStuff"></el-table-column>
            <el-table-column label="申请时间" prop="createTime"></el-table-column>
            <el-table-column label="审核状态" prop="familyStatus"></el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <!-- 回显数据，知道点击按钮所在的这条数据 row就是点击编辑按钮所在的那一行的数据-->
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteFamily(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>

        <!-- 添加编辑家庭弹窗 prop规则对应的属性名-->
<el-dialog v-model="dialogVisible" :title="title" width="30%">
    <el-form :model="familyModel" :rules="rules" label-width="100px" style="padding-right: 30px">
        <el-form-item label="家庭人数" prop="familyNumber">
            <el-input v-model="familyModel.familyNumber" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="家庭收入" prop="familyIncome">
            <el-input v-model="familyModel.familyIncome" minlength="1" maxlength="15"></el-input>
        </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
            <el-button @click="dialogVisible = false">取消</el-button>
            <!-- 通过title的值判断是添加家庭还是编辑家庭 -->
            <el-button type="primary" @click="title==='添加家庭'?addFamily():updateFamily()"> 确认 </el-button>
        </span>
    </template>
</el-dialog> 

<!-- cv -->
<!-- 抽屉 -->
        <el-drawer v-model="visibleDrawer" title="添加文章" direction="rtl" size="50%">
            <!-- 添加文章表单 -->
            <el-form :model="familyModel" label-width="100px" >
                <el-form-item label="家庭人数" >
                    <el-input v-model="familyModel.familyNumber" placeholder="请输入家庭人数"></el-input>
                </el-form-item>
                <el-form-item label="家庭收入" >
                    <el-input v-model="familyModel.familyIncome" placeholder="请输入家庭收入"></el-input>
                </el-form-item>
                <el-form-item label="文章封面">
                    <!-- el-upload自动完成图片上传 -->
                    <!--
                        auto-upload是否自动上传
                        action服务器接口路径
                        name设置上传的文件字段名
                        headers:设置上传的请求头
                        on-success:设置上传成功的回调函数
                    -->
                    <el-upload class="avatar-uploader" :auto-upload="true" :show-file-list="false"
                    action="">
                        <img v-if="familyModel.familyStuff" :src="familyModel.familyStuff" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary">发布</el-button>
                    <el-button type="info">草稿</el-button>
                </el-form-item>
            </el-form>
        </el-drawer>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    //cv
}
.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}
.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>